// Copyright 2007-2010 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
// Supported by Media Technology LTD 
//  
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// MODIFICATIONS HAVE BEEN MADE TO THIS FILE
using MvcContrib.Comdiv.ViewEngines.Brail;
using NUnit.Framework;

namespace Comdiv.Brail.Tests{
    [TestFixture]
    [Category("BrailViewEngine")]
    public class IgnoreNullTester{
        #region Setup/Teardown

        [SetUp]
        public void SetUp() {}

        #endregion

        private class Duck{
            public string Name { get; set; }

            public string this[int index]{
                get { return Name; }
                set { Name = value; }
            }
        }

        [Test]
        public void AreEqual_Returns_Equality_Of_Targets(){
            var ignore1 = new IgnoreNull(1);
            var ignore2 = new IgnoreNull(2 - 1);

            Assert.IsTrue(IgnoreNull.AreEqual(ignore1, ignore2));
        }

        [Test]
        public void Can_Get_With_Parameters(){
            var duck = new Duck{Name = "Donald"};
            var ignore = new IgnoreNull(duck);

            Assert.AreEqual(duck.Name, ignore.QuackGet("Item", new object[1]{1}).ToString());
        }

        [Test]
        public void Can_Set_With_Parameters(){
            var duck = new Duck();
            var ignore = new IgnoreNull(duck);

            ignore.QuackSet("Item", new object[1]{1}, "Donald");

            Assert.AreEqual("Donald", duck.Name);
        }

        [Test]
        public void Returns_Itself_For_Null_Targets(){
            var ignore = new IgnoreNull(null);

            Assert.AreEqual(ignore, ignore.QuackGet(null, null));
            Assert.AreEqual(ignore, ignore.QuackSet(null, null, null));
            Assert.AreEqual(ignore, ignore.QuackInvoke(null, null));
        }

        [Test]
        public void ToString_Returns_Empty_String_For_Null_Target(){
            var ignore = new IgnoreNull(null);

            Assert.AreEqual(string.Empty, ignore.ToString());

            ignore = new IgnoreNull(1);
            Assert.AreEqual("1", ignore.ToString());
        }

        [Test]
        public void Wraps_Result_In_IgnoreNull(){
            var ignore = new IgnoreNull(new Duck());

            Assert.IsInstanceOf<IgnoreNull>(ignore.QuackGet("Name", null));
            Assert.IsInstanceOf<IgnoreNull>(ignore.QuackSet("Name", null, "Donald"));
            Assert.IsInstanceOf<IgnoreNull>(ignore.QuackInvoke("ToString", null));

            Assert.AreEqual(true, ignore.QuackGet("_IsIgnoreNullReferencingNotNullObject_", null));
        }
    }
}